<?php

namespace Frieza\Modules\Photo\Extensions;

use Doraemon\model\AdmUserType;
use Doraemon\model\BadgeType;
use Doraemon\model\Content\ContentCategoryLabel as mContentCategoryLabel;
use Doraemon\model\Member;
use Doraemon\model\Pics;
use Doraemon\model\TopicActivity;
use Doraemon\service\AdminTag\Schedule;
use Doraemon\service\Photos;
use Doraemon\service\PhotoScreen;
use Encore\Admin\Grid;
use Illuminate\Http\Request;

class Filter
{
    const BADGE_GROUP = [
        2,
        4
    ];
    const DEFAULT_SEARCH_PHOTO_TYPE = Schedule::SEARCH_PHOTO_TYPE_PHOTO;
    const DEFAULT_IS_AUDIT_AGAIN = Schedule::NO_SEE_AUDIT_AGAIN;
    const DEFAULT_SEARCH_TYPE = Schedule::SEARCH_TYPE_REMARK_AND_TAG;
    const DEFAULT_TAG_STATUS = Schedule::TAG_STATUS_ALL;
    const DEFAULT_PHOTO_NUM = Schedule::PHOTO_NUM_ALL;
    const DEFAULE_RELATE_GOODS = Schedule::DISINTEREST_RELATED_GOODS;
    const DEFAULT_MAIN = Schedule::SEARCH_MAIN_NOTE;
    const DEFAULE_SUSPECTED_VIOLATION_TYPE = Schedule::SUSPECTED_VIOLATION_TYPE_ALL;
    const DEFAULE_PER_PAGE = 20;
    const DEFAULT_ONLY_NOTE_HAS_SPACE_TAG = 0;

    private static function getFilterBadgeList()
    {
        $filterBadges = BadgeType::getBadgeInfoByGroup(self::BADGE_GROUP);
        return array_column($filterBadges, 'badge_name', 'badge_name');
    }

    public static function getFilter(Grid\Filter $filter, $withSuspectedViolation = false)
    {
        $categoryFirstId = request('category_first_id', null);
        $categoryLabelSecond = [];
        if ($categoryFirstId) {
            $categoryLabelSecond = mContentCategoryLabel::getListFormatKey($categoryFirstId, 2);
        }
        $categoryLabelFirst = mContentCategoryLabel::getListFormatKey(0, 1);
        $categoryLabelFirst[0] = '未打标签';
        ksort($categoryLabelFirst);
        $filter->disableIdFilter();
        $filter->column(1 / 2, function ($filter) use ($withSuspectedViolation) {
            $filter->equal('user_type', '用户身份')->checkbox(Member::$typeDesc);
            //$filter->equal('badge', '用户徽章')->checkbox(self::getFilterBadgeList());
            $filter->equal('confirmed', '待确认身份')->checkbox(AdmUserType::AUTH_TAG_DESC);
//            $filter->equal('high_check', '高级筛选')->checkbox(Schedule::ADVANCED_FILTER_DESC);
            $filter->equal('search_photo_type', '内容类型')->radio(Schedule::SEARCH_NOTE_TYPE_DESC);
            $filter->equal('relate_goods', '关联WiKi')->radio(Schedule::RELATED_GOODS_DESC);
            $filter->equal('is_audit_again', '复审note')->radio(Schedule::AUDIT_AGAIN_DESC);
            $filter->equal('main', '内容主体')->checkbox(Schedule::SEARCH_MAIN_DESC);
            if ($withSuspectedViolation) {
                $filter->equal('is_suspected_violation', '疑似违规状态')->radio(Schedule::SUSPECTED_VIOLATION_TYPE_DESC);
            }
            $filter->equal('content_label', '内容标签')->radio([1 => "全部", 2 => "已有标签", 0 => "未打标签"]);
            $filter->equal('business', '商业')->radio(Schedule::BUSINESS);
        });

        $filter->column(1 / 2, function ($filter) use ($categoryLabelFirst,$categoryLabelSecond) {
            $filter->dropDown('time', [
                'release_time' => '发图时间',
            ])->betweenDatetime();
            $filter->dropDown('search_id', [
                'photo_id' => 'Note id',
                'uid'      => '用户uid',
                'pic_id'   => 'Pic id',
                'nick' => '用户昵称'
            ]);
            $filter->dropDown('search_keyword', [
                'search_tag'  => '搜索词',
                'topic_title' => '话题标题',
            ]);
            $filter->equal('search_type', '搜索方式')->checkbox(Schedule::SEARCH_TYPE_DESC);
            $filter->equal('score', '分数')->checkbox(Schedule::SCORE_DESC);
            $filter->equal('tag_status', '标签状态')->radio(Schedule::TAG_STATUS_DESC);
            $filter->equal('photo_num', '图片数')->radio(Schedule::PHOTO_NUM_DESC);
//            $filter->equal('is_consume_goods','消费内容')->radio(Schedule::CONSUME_MAP);
//            $filter->in('category', '品类')->multipleSelect(function (){
//                $categorys = ContentCategory::getDimensionCategorys();
//                $data = ['0' => '未打品类'];
//                foreach ($categorys as $category) {
//                    $data[$category['category']] = $category['category'];
//                }
//                return $data;
//            });

            $filter->equal('category_first_id', '一级品类')->select($categoryLabelFirst)->load('category_second_id', route('getCategoryLabel'));
            $filter->equal('category_second_id', '二级品类')->select($categoryLabelSecond);

        });
    }

    private static function getDefaultParams(Request $request, $withSuspectedViolation = false)
    {
        $params = $request->all();
        if (!isset($params['search_photo_type'])) {
            $request->merge([
                'search_photo_type' => (string)self::DEFAULT_SEARCH_PHOTO_TYPE
            ]);
        }
        if (!isset($params['audit_again'])) {
            $request->merge([
                'audit_again' => (string)self::DEFAULT_IS_AUDIT_AGAIN
            ]);
        }
//        if (!isset($params['search_type'])) {
//            $request->merge(['search_type' => (string)self::DEFAULT_SEARCH_TYPE]);
//        }
        if (!isset($params['tag_status'])) {
            $request->merge(['tag_status' => (string)self::DEFAULT_TAG_STATUS]);
        }
        if (!isset($params['photo_num'])) {
            $request->merge(['photo_num' => (string)self::DEFAULT_PHOTO_NUM]);
        }
        if (!isset($params['relate_goods'])) {
            $request->merge([
                'relate_goods' => (string)self::DEFAULE_RELATE_GOODS
            ]);
        }
        if(!isset($params['main'])) {
            $request->merge(['main' => [self::DEFAULT_MAIN]]);
        }
        if (!isset($params['release_time'])) {
            $request->merge([
                'release_time' => [
                    'start' => date('Y-m-d H:i:s', strtotime("-1days")),
                    'end'   => date('Y-m-d H:i:s'),
                ]
            ]);
        }
        if (!isset($params['per_page'])) {
            $params['per_page'] = (string)self::DEFAULE_PER_PAGE;
        }
        $params['only_note_has_space_tag'] = (string)self::DEFAULT_ONLY_NOTE_HAS_SPACE_TAG;

        if ($withSuspectedViolation) {
            $request->merge([
                'is_suspected_violation' => (string)self::DEFAULE_SUSPECTED_VIOLATION_TYPE
            ]);
        }
        return $request;
    }

    public static function getSearchResultByParams(Request $request, $withSuspectedViolation = false)
    {
        $defaultResult = [
            'rows'    => [],
            'total'   => 0,
            'pic_num' => 0
        ];
        $request = self::getDefaultParams($request, $withSuspectedViolation);
        $params = $request->all();
        if (isset($params['pic_id']) && $params['pic_id']) {
            $picInfos = Pics::getINFOSByPicId($params['pic_id']);
            if ($picInfos) {
                $params['photo_id'] = $picInfos[0]['note_id'];
            } else {
                return $defaultResult;
            }
        }
        if (isset($params['photo_id']) && $params['photo_id']) {
            $photoInfo = Photos::getPhoto($params['photo_id']);
            if ($photoInfo) {
                return [
                    'rows'    => [$photoInfo['id']],
                    'total'   => 1,
                    'pic_num' => $photoInfo['pic_num']
                ];
            } else {
                return $defaultResult;
            }
        }

        //处理搜索条件
        $object = new PhotoScreen($params);
        //查询数据
        $result = Schedule::getPhotoCheckList($object, $withSuspectedViolation ? Schedule::TAB_VIOLATION : Schedule::TAB_COMMON);
        $result['rows'] = $result['rows'] ? array_column($result['rows'], 'id') : [];
        $result['total'] = (int)$result['total'];
        $request['pic_num'] = (int)$result['pic_num'];
        return $result;
    }

}
